package vn.edu.vttu.goidien;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class CallActivity extends Activity implements android.view.View.OnClickListener{


	//Bien toan cuc
	private Button Button00,Button01,Button02,Button03,Button04,Button05,ButtonDel,
					Button06,Button07,Button08,Button09,ButtonStar,ButtonPound,ButtonGoi,ButtonThoat;
	private TextView Number;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_call);
		ThamChieu();
		
	}
	
	private void ThamChieu()
	{
		Number = (TextView)findViewById(R.id.Number);
		Button00 = (Button)findViewById(R.id.Button00);
		Button00.setOnClickListener(this);
		Button01 = (Button)findViewById(R.id.Button01);
		Button01.setOnClickListener(this);
		Button02 = (Button)findViewById(R.id.Button02);
		Button02.setOnClickListener(this);
		Button03 = (Button)findViewById(R.id.Button03);
		Button03.setOnClickListener(this);
		Button04 = (Button)findViewById(R.id.Button04);
		Button04.setOnClickListener(this);
		Button05 = (Button)findViewById(R.id.Button05);
		Button05.setOnClickListener(this);
		Button06 = (Button)findViewById(R.id.Button06);
		Button06.setOnClickListener(this);
		Button07 = (Button)findViewById(R.id.Button07);
		Button07.setOnClickListener(this);
		Button08 = (Button)findViewById(R.id.Button08);
		Button08.setOnClickListener(this);
		Button09 = (Button)findViewById(R.id.Button09);
		Button09.setOnClickListener(this);
		ButtonDel = (Button)findViewById(R.id.ButtonDel);
		ButtonDel.setOnClickListener(this);
		ButtonPound = (Button)findViewById(R.id.ButtonPound);
		ButtonPound.setOnClickListener(this);
		ButtonStar	= (Button)findViewById(R.id.ButtonStar);
		ButtonStar.setOnClickListener(this);
		ButtonGoi = (Button)findViewById(R.id.ButtonGoi);
		ButtonGoi.setOnClickListener(this);
		ButtonThoat = (Button)findViewById(R.id.ButtonThoat);
		ButtonThoat.setOnClickListener(this);
	}
	
	/*
	//Lay gia tri button khi click vao TextView
	private void Button00() {
		// TODO Auto-generated method stub
		Number.setText(Button00.getText().toString());
	}
	private void Button01() {
		// TODO Auto-generated method stub
		Number.setText(Button01.getText().toString());
	}
	private void Button02() {
		// TODO Auto-generated method stub
		Number.setText(Button02.getText().toString());
	}
	private void Button03() {
		// TODO Auto-generated method stub
		Number.setText(Button03.getText().toString());
	}
	private void Button04() {
		// TODO Auto-generated method stub
		Number.setText(Button04.getText().toString());
	}
	private void Button05() {
		// TODO Auto-generated method stub
		Number.setText(Button05.getText().toString());
	}
	private void Button06() {
		// TODO Auto-generated method stub
		Number.setText(Button06.getText().toString());
	}
	private void Button07() {
		// TODO Auto-generated method stub
		Number.setText(Button07.getText().toString());
	}
	private void Button08() {
		// TODO Auto-generated method stub
		Number.setText(Button08.getText().toString());
	}
	private void Button09() {
		// TODO Auto-generated method stub
		Number.setText(Button09.getText().toString());
	}
	private void ButtonStar() {
		// TODO Auto-generated method stub
		Number.setText(ButtonStar.getText().toString());
	}
	private void ButtonPound() {
		// TODO Auto-generated method stub
		Number.setText(ButtonPound.getText().toString());
	}
	*/

	 void addText(char newChar){
         CharSequence start = Number.getText();
         String ret = start.toString();
         ret +=newChar;
         Number.setText(ret,BufferType.EDITABLE);
         //dialNumber.setInputType(InputType.TYPE_CLASS_PHONE);
	 }
	 void delText(){
         CharSequence start = Number.getText();
         String ret = start.toString();
         int len = ret.length();
         if(len>0){
                 ret = ret.substring(0, len-1);
         }
         //ret.f
         Number.setText(ret,BufferType.EDITABLE);
         //dialNumber
         Number.setInputType(InputType.TYPE_CLASS_PHONE);
 }
	 public void ButtonGoi() {
		// TODO Auto-generated method stub
		 String phone_no = Number.getText().toString();
		 Intent i = new Intent(Intent.ACTION_CALL);
		 i.setData(Uri.parse("tel:"+phone_no));
		 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		 startActivity(i);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//SetText 
		if (v.getId() == Button00.getId()){ addText('0');}
		else if(v.getId() == Button01.getId()){addText('1');}
		else if(v.getId() == Button02.getId()){addText('2');}
		else if(v.getId() == Button03.getId()){addText('3');}
		else if(v.getId() == Button04.getId()){addText('4');}
		else if(v.getId() == Button05.getId()){addText('5');}
		else if(v.getId() == Button06.getId()){addText('6');}
		else if(v.getId() == Button07.getId()){addText('7');}
		else if(v.getId() == Button08.getId()){addText('8');}
		else if(v.getId() == Button09.getId()){addText('9');}
		else if(v.getId() == ButtonStar.getId()){addText('*');}
		else if(v.getId() == ButtonPound.getId()){addText('#');}
		else if(v.getId() ==  ButtonDel.getId()){ delText(); }
		
		if(ButtonGoi.isPressed())
		{
			ButtonGoi();
		}
		else if(ButtonThoat.isPressed())
		{
			finish();
			System.exit(0);
		}
	}
}
